<template>
  <div class="process-m1">
    <van-field
      v-model="message"
      rows="5"
      autosize
      maxlength="200"
      label=""
      type="textarea"
      placeholder="请输入内容"
    />
    <div @click="onClickRight" class="operator-bar">
      <van-button :loading="loading" type="primary">完成</van-button>
    </div>
  </div>
</template>
<script>
/* eslint-disable */
import Bus from '@/utils/bus'
import process from '@/api/process'
import { Dialog } from 'vant';
import { getParameterByName } from '@/utils/util'
export default {
  components: {
    [Dialog.Component.name]: Dialog.Component,
  },
  data () {
    return {
      loading: false,
      message: '',
      radio: '',
      step: getParameterByName('route'),
      taskId: getParameterByName('taskId'),
    }
  },
  mounted () {
    Bus.$on('nextSubmitMobile',() =>{
      console.log('可以真正提交了');
      this.submitData();
    });
  },
  methods: {
    onClickLeft(){
      this.$router.push({ path: '/process-tpl' + window.location.search }) 
    },
    onClickRight(){
      Dialog.confirm({
        title: '确认提交吗？',
      })
      .then(() => {
        const that = this;
        that.loading = true;
        // 通知表单2保存数据
        if ( this.step && (this.step === 'b') ){
          Bus.$emit('prevSubmitMobile',this.$route.query.radio);
        }
        if ( (this.step && (this.step === 'c')) || (+this.$route.query.radio === 12) ){
          this.submitData();
        }
      })
      .catch(() => {
        // on cancel
      });
    },
    submitData(){
      const params = {
        // 处理意见
        comment: this.message,
        // 当前任务ID
        taskId: this.taskId,
        assigneeId: "",
        variables: {}
      };
      if (+this.$route.query.radio === 3) {
        process.completeTask(params).then(res => {
          if ( res.code === 0 ) {
            this.$message.success('同意操作提交成功');
            window.history.go(-2);
          }else {
            this.$message.error(res.msg);
          }
          this.loading = false;
        }).catch( r =>{
          this.loading = false;
        });
      } else if (+this.$route.query.radio === 6) {
        process.rejectTask(params).then(res => {
          if ( res.code === 0 ) {
            this.$message.success('驳回操作提交成功');
            window.history.go(-2);
          }else {
            this.$message.error(res.msg);
          }
          this.loading = false;
        }).catch( r =>{
          this.loading = false;
        });
      } else if (+this.$route.query.radio === 12) {
        return process.retractTask(params).then(res => {
          if (res.code === 0) {
            this.$message.success('撤回操作提交成功')
            window.history.go(-2);
          } else {
            this.$message.error(res.msg)
          }
          this.loading = false;
        }).catch( r =>{
          this.loading = false;
        });
      }
    },
  }
}
</script>

<style lang="less">
body{
  min-width: auto;
}
.process-m1{
  .van-field__control{
    border: 1px solid #f2f2f2;
    border-radius: 2px;
    padding: 4px;
    box-sizing: border-box;
    position: relative;
  }
  .operator-bar{
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    bottom: 0;
    height: 60px;
    width: 100%;
    border-top: 1px solid #f2f2f2;
    background: #fff;
    color: #409EFD;
    cursor: pointer;
    .van-button{
      color: #409EFD;
      width: 100%;
      background-color: #fff;
      border-color: #fff;
    }
  }
}
</style>
